'use strict';
const Controller = require('egg').Controller;

class MassageController extends Controller {
    //查询
    async index(ctx) {

        const data = await ctx.service.massage.findAll();
        if (data) {
            ctx.body = {
                code: 200,
                msg: "全表数据查询成功",
                data
            }
        }
        else {
            ctx.body = {
                code: 404,
                msg: "全表数据查询失败",
            }
        }
    }
    async item(ctx) {
        let { id } = this.ctx.params;
        console.log(id);
        const data = await ctx.service.massage.finditem(id);
        if (data) {
            ctx.body = {
                code: 200,
                msg: "单条数据查询成功",

                data
            }
        }
        else {
            ctx.body = {
                code: 404,
                msg: "单条数据查询失败",

            }
        }
    }
    async update(ctx) {
        let { id } = this.ctx.params;
        let { name, tel, address } = this.ctx.request.body;
        if (name && tel && address) {
            const item = {
                id,
                name,
                tel,
                address,
            }
            let data = await ctx.service.massage.exit(item);
            if (data.data) {
                ctx.body = {
                    code: data.code,
                    msg: data.msg,
                }
            }
            else {
                ctx.body = {
                    code: data.code,
                    msg: data.msg,
                }
            }
        }
        else {
            ctx.body = {
                code: 404,
                msg: "为获取到参数"
            }
        }

    }
    async create(ctx) {
        let { name, tel, address } = this.ctx.request.body;
        console.log(name, tel, address);
        if (name && tel && address) {
            let data = await ctx.service.massage.add(this.ctx.request.body);
            if (data.code === 200) {
                ctx.body = {
                    code: 200,
                    msg: "添加成功"
                }
            }
            else {
                ctx.body = {
                    code: 404,
                    msg: "添加失败"
                }
            }
        }
        else {
            console.log("请输入参数")
        }

    }
    async destroy(ctx){
        let {id}=this.ctx.params;
        console.log(id);
        let data= await ctx.service.massage.del(id);
           if(data.msg==="删除成功"){
            ctx.body={
                code:data.code,
                msg:data.msg,
            }
        }
        else{
            ctx.body={
                code:data.code,
                msg:data.msg,
            }
        }
        
    }

}

module.exports = MassageController;
